function validateSignup() {
    if (checkUsername() &&
        checkPasswords() &&
        checkNode() &&
        checkLocation() &&
        checkTerms()) {
            $('signupForm').submit();
    }
}

function checkUsername() {
    var u = $('user-url');
    var uv = $('username');
    var ua = $('user-available');
    var u3 = $('user-3chars');
    var us = $('user-spaces');
    u.innerHTML = uv.value;

    if (uv.value.indexOf(' ') >= 0) {
        ua.innerHTML = '';
        u.setAttribute('class', 'red');
        us.setAttribute('class', 'red');
        ua.setAttribute('class', 'red');
        return false;
    } else {
        us.setAttribute('class', 'green');
    }
    
    var re = new RegExp('^[a-zA-Z_]+$');
    if (!uv.value.match(re)) {
        $('user-signs').innerHTML = 'Username can only contain letters, numbers and \'_\', ';
        u.setAttribute('class', 'red');
        ua.setAttribute('class', 'red');
        return false;
    } else {
        $('user-signs').innerHTML = '';
    }

    if (uv.value.length < 3) {
        ua.innerHTML = '';
        u.setAttribute('class', 'red');
        u3.setAttribute('class', 'red');
        ua.setAttribute('class', 'red');
        return false;
    } else {
        u3.setAttribute('class', 'green');
    }

    if (usernameAvailable(uv.value)) {
        ua.innerHTML = 'Available!';
        ua.setAttribute('class', 'green');
        u.setAttribute('class', 'green');
    } else {
        ua.innerHTML = 'That username has been taken';
        u.setAttribute('class', 'red');
        ua.setAttribute('class', 'red');
        return false;
    }
    return true;
}

function usernameAvailable(user) {
    return true;
}


function checkPasswords() {
    if ($('password').value != $('confirm').value) {
        alert('Passwords doesn\'t match');
        $('password').setAttribute('class', 'field-error');
        $('password').focus();
        return false;
    }
    $('password').setAttribute('class', '');
    return true;
}

function checkNode() {
    if ($('node').value == '') {
        alert('You must provide your node name');
        $('node').setAttribute('class', 'field-error');
        $('node').focus();
        return false;
    }
    $('node').setAttribute('class', '');
    return true;
}

function checkLocation() {
    if ($('location').value == '') {
        alert('You must provide your location');
        $('location').setAttribute('class', 'field-error');
        $('location').focus();
        return false;
    }
    $('location').setAttribute('class', '');
    return true;
}

function checkTerms() {
    if (!$('tos').checked) {
        alert('You must agree with terms and privacy policy to sign up');
        return false;
    }
    return true;
}
